/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package im.clusterhead;

import im.general.AbstractListenBehaviour;
import im.general.Config;
import im.messages.BaseMessage;
import im.messages.SubscriptionMessage;
import im.node.MobileNodeInfo;
import im.subscriptionpackage.ContentSubscription;
import jade.lang.acl.ACLMessage;
import jade.lang.acl.MessageTemplate;
import jade.lang.acl.UnreadableException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author brownstone
 */
public class SubscribeListenBehaviour extends AbstractListenBehaviour {

    ClusterHeadAgent ch;

    public SubscribeListenBehaviour(ClusterHeadAgent ch) {
        this.ch = ch;
         mt = MessageTemplate.MatchEncoding(Config.SUBSCRIBE_NAME);
    }

    @Override
    protected void handleMessage(ACLMessage msg) {

        SubscriptionMessage smsg = null;

        try {
            //System.out.println("SubscribeListen handling");
            smsg = (SubscriptionMessage) msg.getContentObject();
        } catch (UnreadableException ex) {
            Logger.getLogger(SubscribeListenBehaviour.class.getName()).log(Level.SEVERE, null, ex);
        }

        ContentSubscription contentSubscription = (ContentSubscription) smsg.getSubscription();
        ch.setContentSubscription(msg.getSender().getName(), contentSubscription);
    }
}
